﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using RedHouse.Data.Service;
using System.Net.Mail;
using RedHouse.Data.Common;

namespace RedHouse.Data.Base
{
    public class EmailService : IEmailService
    {
        private static IEmailService _instance;
        public static IEmailService Instance
        {
            get
            {
                if (_instance == null)
                {
                    _instance = new EmailService();
                }
                return _instance;
            }
        }

        public MailMessage MsgMail { get; set; }
        public SmtpClient Client { get; set; }
        public EmailService()
        {

        }


        #region IEmailService Members

        public Common.OperationResult SendEmail(Entities.RedHouseEmail email)
        {
            OperationResult result = new OperationResult()
            {
                Message = string.Empty,
                Result = true
            };
            try
            {
                MsgMail = new MailMessage();
                Client = new SmtpClient();
                Client.Credentials = new System.Net.NetworkCredential(email.From, email.Password);

                Client.Port = 587;
                Client.Host = "smtp.gmail.com"; //Or Your SMTP Server Address
                Client.Credentials = new System.Net.NetworkCredential
                     ("redhousevietnam@gmail.com", "Hamlong2011");
                //Or your Smtp Email ID and Password
                Client.EnableSsl = true;

                MsgMail.Subject = email.Subject;

                foreach (string item in email.ToList)
                {
                    MsgMail.To.Add(item);
                }

                foreach (string item in email.CcList)
                {
                    MsgMail.CC.Add(item);
                }

                foreach (string item in email.BccList)
                {
                    MsgMail.Bcc.Add(item);
                }

                MsgMail.IsBodyHtml = true;

               // MsgMail.Body = email.Body;

                string bodyPlain = email.PlainBody;

                AlternateView htmlView = AlternateView.CreateAlternateViewFromString(email.Body, null, "text/html");
                AlternateView plainView = AlternateView.CreateAlternateViewFromString(bodyPlain, null, "text/plain");
                MsgMail.AlternateViews.Add(htmlView);
                MsgMail.AlternateViews.Add(plainView);

                MsgMail.From = new MailAddress(email.From);
                Object state = MsgMail;

                //event handler for asynchronous call
                Client.SendCompleted += new SendCompletedEventHandler(Client_SendCompleted);
                Client.SendAsync(MsgMail, state);

            }
            catch (Exception ex)
            {
                result.Message = ex.Message;
                result.Result = false;
            }
            return result;
        }

        void Client_SendCompleted(object sender, System.ComponentModel.AsyncCompletedEventArgs e)
        {
            return;
        }

        #endregion
    }
}
